package com.递归2;


import java.util.List;

class ListNode {
    int val;
    ListNode next;
    ListNode(int x){
        this.val = x;
    }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }

    ListNode(int[] nums){
       this(-1);
        ListNode first =  this;

        for (int num : nums) {
            first.next = new ListNode(num);
            first = first.next;
        }

    }

}
 public class MyLinkedList {
    //哨兵节点
    ListNode head ;
    int size ;
    public  MyLinkedList(){
        this.head = new ListNode(0);
        this.size = 0;
    }
    //可以添加数组。
    public MyLinkedList(int[] nums){
        this(); //调用构造器
        for(int i : nums){
            this.addAtTail(i);
        }
    }
    //如果索引无效，则返回-1。
    public int get(int index) {
        if(index < 0 || index >=size){
            return -1;
        }

        ListNode node = head;
        for(int i = 0;i<index+1;i++){
            node = node.next;
        }
        return node.val;
    }

    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    public void addAtHead(int val) {
        addAtIndex(0,val);
    }

    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        addAtIndex(size,val);

    }

    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    public void addAtIndex(int index, int val) {
        if(index > size ){
            return;
        }
        if(index < 0){
            index  = 0;
        }
        size++;
        ListNode node = head;
        //此处要满足三个需求，1，添加头部，2.添加尾部，
        // 3，添加中间：如果 index 等于链表的长度，则该节点将附加到链表的末尾。
        // 如果 index 大于链表长度，则不会插入节点。
        // 如果index小于0，则在头部插入节点。
        for(int i  = 0 ;i<index;i++){
            node = node.next;
        }
        ListNode add = new ListNode(val);
        add.next = node.next;
        node.next = add;
    }

    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        ListNode node = head;
        if(index >= size || index < 0){
            return;
        }
        size--;
        for(int i = 0;i<index;i++){
            node = node.next;
        }
        node.next = node.next.next;


    }
    //添加环形链表  添加后不能遍历此链表，会进入死循环。
    public void addCycle(){
        ListNode node = head;
        ListNode tmp  = head;
        for(int  i = 0;i<this.size;i++){
            node = node.next;
            tmp = tmp.next;
        }
        node.next = tmp;

    }
    public String toString(){
        StringBuilder builder = new StringBuilder();
        ListNode node = head;
        builder.append(String.format("容量:%s，链表{",size));
        while (node != null){
            builder.append(String.format("[value:%s]",node.val));
            node = node.next;
        }
        builder.append("}");
        return builder.toString();
    }
    public static void main(String[] args) {
        MyLinkedList link = new MyLinkedList();
//        System.out.println(link.toString());
//        link.addAtHead(1);
//        link.addAtTail(3);
//        link.addAtIndex(1,2);
//        System.out.println(link.toString());
//        System.out.println( link.get(1));
//        link.deleteAtIndex(1);
//        System.out.println(link.toString());
//        System.out.println( link.get(1));
//        System.out.println(link.toString());
        int[] nums = {1};
        MyLinkedList link2 = new MyLinkedList(nums);
        System.out.println(link2.toString());
        ListNode node = link2.head;
        ListNode tmp = link2.head;
        for(int i = 0;i<link2.size;i++){
            node = node.next;
            tmp = tmp.next;
        }
        node.next = tmp;
        System.out.println(link2.toString());


    }
}
